package com.system.dao;

import com.am.base.BaseDao;
import com.system.uitls.CommonUtil;
import com.system.uitls.IdUtils;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class RegisterDao extends BaseDao
{
  public List<Map<String, Object>> getRegisterList()
  {
    String sql = "SELECT id,register_name,create_time,register_charge,book_amount,create_user_id,create_user_name,status,back_reason,amount FROM sys_register";

    return getSimpleJdbc().queryForList(sql, new Object[0]);
  }

  public Map<String, Object> findRegister(String id)
  {
    String sql = "SELECT register_name,create_time,register_charge,book_amount,create_user_id,create_user_name,status,back_reason,amount FROM sys_register WHERE id=?";
    Map map = getSimpleJdbc().queryForMap(sql, new Object[] { id });

    return map;
  }

  public Map<String, String> updatePrivate(Map<String, Object> map)
  {
    String register_name = CommonUtil.nullToEmptyString(map.get("register_name"));
    String sex = CommonUtil.nullToEmptyString(map.get("sex"));
    String address = CommonUtil.nullToEmptyString(map.get("address"));
    String birthday = CommonUtil.nullToEmptyString(map.get("birthday"));
    String id_card = CommonUtil.nullToEmptyString(map.get("IDnumber"));

    String person_type = "4";
    String telephone = CommonUtil.nullToEmptyString(map.get("telephone"));
    int register_charge = Integer.parseInt(CommonUtil.nullToEmptyString(map.get("registerCost")));
    int book_amount = CommonUtil.convertToInt(CommonUtil.nullToEmptyString(map.get("book_amount")), 0);
    String create_user_name = CommonUtil.nullToEmptyString(map.get("currentUserName"));
    String create_user_id = CommonUtil.nullToEmptyString(map.get("create_user_id"));
    String campus = CommonUtil.nullToEmptyString(map.get("currentUserCampus"));
    String create_time = CommonUtil.getCurDate();

    Map result = new HashMap();
    List sqlList = new ArrayList();
    List params = new ArrayList();
    try
    {
      String person_id = IdUtils.getSequence(getJdbc(), "sys_person_id");
      sqlList.add("INSERT INTO sys_person(id,name,sex,birthday,type,id_card,address,telephone,is_overdue,status) VALUES(?,?,?,?,?,?,?,?,?,?)");
      params.add(new Object[] { person_id, register_name, sex, birthday, person_type, id_card, address, telephone, "1", "1" });

      String id = IdUtils.getSequence(getJdbc(), "sys_register_id");
      sqlList.add("INSERT INTO sys_register(id,create_time,register_name,register_id,create_user_name,create_user_id,register_charge,book_amount,status,person_campus,amount) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
      params.add(new Object[] { id, create_time, register_name, person_id, create_user_name, create_user_id, Integer.valueOf(register_charge), Integer.valueOf(book_amount), "1", campus, Integer.valueOf(book_amount + register_charge) });

      String record_id = IdUtils.getSequence(getJdbc(), "sys_patient_record_id");
      sqlList.add("INSERT INTO sys_patient_record(id,register_id,register_type,person_name,person_id,person_campus,status,create_time) VALUES(?,?,?,?,?,?,?,?)");
      params.add(new Object[] { record_id, id, "1", register_name, person_id, campus, "1", create_time });

      batchInTransaction(sqlList, params);
      result.put("type", "success");
      result.put("value", "挂号成功");
    } catch (Exception ex) {
      logger.error("RegisterDao[updatePrivate]-->sql:" + sqlList.toString() + "\n" + ex.getMessage());
    }

    return result;
  }

  public Map<String, String> updatePublic(Map<String, Object> map)
  {
    String personId = CommonUtil.nullToEmptyString(map.get("personId"));
    String create_time = CommonUtil.getCurDate();
    String register_name = CommonUtil.nullToEmptyString(map.get("name"));
    String create_user_name = CommonUtil.nullToEmptyString(map.get("currentUserName"));
    String create_user_id = CommonUtil.nullToEmptyString(map.get("create_user_id"));

    String register_charge = CommonUtil.nullToEmptyString(map.get("register_charge"));
    String book_amount = CommonUtil.nullToEmptyString(map.get("book_amount"));
    String status = CommonUtil.nullToEmptyString(map.get("status"));
    String amount = CommonUtil.nullToEmptyString(map.get("amount"));
    String campus = CommonUtil.nullToEmptyString(map.get("currentUserCampus"));
    String register_type = CommonUtil.nullToEmptyString(map.get("register_type"));

    Map result = new HashMap();
    List sqlList = new ArrayList();
    List params = new ArrayList();
    try
    {
      String id = IdUtils.getSequence(getJdbc(), "sys_register_id");
      sqlList.add("INSERT INTO sys_register(id,create_time,register_name,register_id,create_user_name,create_user_id,register_charge,book_amount,status,person_campus,amount) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
      params.add(new Object[] { id, create_time, register_name, personId, create_user_name, create_user_id, register_charge, book_amount, status, campus, amount });

      String record_id = IdUtils.getSequence(getJdbc(), "sys_patient_record_id");
      sqlList.add("INSERT INTO sys_patient_record(id,register_id,register_type,person_name,person_id,person_campus,status,create_time) VALUES(?,?,?,?,?,?,?,?)");
      params.add(new Object[] { record_id, id, register_type, register_name, personId, campus, status, create_time });

      batchInTransaction(sqlList, params);
      result.put("type", "success");
      result.put("value", "挂号成功");
    }
    catch (Exception ex) {
      logger.error("RegisterDao[updatePublic]-->sql:" + sqlList.toString() + "\n" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "挂号失败");
    }
    return result;
  }

  public Map<String, String> updateStatus(Map<String, Object> map)
  {
    String id = CommonUtil.nullToEmptyString(map.get("id"));
    Map result = new HashMap();
    List sqlList = new ArrayList();
    List params = new ArrayList();
    try
    {
      String back_reason = CommonUtil.nullToEmptyString(map.get("back_reason"));
      sqlList.add("UPDATE sys_register SET status=?,back_reason=? WHERE id=?");
      params.add(new Object[] { "0", back_reason, id });

      sqlList.add("DELETE FROM sys_patient_record WHERE register_id=?");
      params.add(new Object[] { id });

      batchInTransaction(sqlList, params);

      result.put("type", "success");
      result.put("value", "退号成功");
    }
    catch (Exception ex) {
      System.out.println(ex.getMessage());
      result.put("type", "fail");
      result.put("value", "退号失败");
    }

    return result;
  }

  public String checkEnableBack(String id)
  {
    int status = getSimpleJdbc().queryForInt("SELECT status from sys_patient_record where register_id=?", new Object[] { id });
    if (status == 1)
    {
      return "1";
    }
    return "0";
  }
}